package com.lottery.ruok.lottrey.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.lottery.ruok.lottrey.retrofitModule.response.ActivityListResponse;
import com.lottery.ruok.lottrey.utils.DisplayUtil;

public class AlterableProgressBar extends ProgressBar {
    private Context mContext;

    public AlterableProgressBar(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public AlterableProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public AlterableProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {

    }

    /**
     * 动态设置ProgressBar颜色
     *
     * @param missionsBean
     */
    public void setProgressBarStyle(ActivityListResponse.ListBean.MissionsBean missionsBean, int index) {
        float radius0;
        float[] outerR;
        if (index == 0) {
            radius0 = DisplayUtil.dip2px(5, mContext);
            outerR = new float[]{radius0, radius0, 0, 0, 0, 0, radius0, radius0};
        } else {
            outerR = null;
        }

        RoundRectShape roundRectShape1 = new RoundRectShape(outerR, null, null);
        ShapeDrawable shapeDrawable1 = new ShapeDrawable();
        shapeDrawable1.setShape(roundRectShape1);
        shapeDrawable1.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable1.getPaint().setColor(Color.parseColor(missionsBean.getProgressbar().getProgress_color()));
        ClipDrawable clipDrawable = new ClipDrawable(shapeDrawable1, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        RoundRectShape roundRectShape0 = new RoundRectShape(outerR, null, null);
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        shapeDrawable.setShape(roundRectShape0);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable.getPaint().setColor(Color.parseColor(missionsBean.getProgressbar().getDefault_color()));

        Drawable[] layers = new Drawable[]{shapeDrawable, clipDrawable};
        LayerDrawable layerDrawable = new LayerDrawable(layers);
        layerDrawable.setId(0, android.R.id.background);
        layerDrawable.setId(1, android.R.id.progress);
        setProgressDrawable(layerDrawable);
    }

    /**
     * 动态设置ProgressBar颜色 Single mission专用
     *
     * @param missionsBean
     */
    public void setProgressBarStyle(ActivityListResponse.ListBean.MissionsBean missionsBean) {
        float radius0 = DisplayUtil.dip2px(5, mContext);
        float[] outerR = new float[]{radius0, radius0, radius0, radius0, radius0, radius0, radius0, radius0};
        RoundRectShape roundRectShape1 = new RoundRectShape(outerR, null, null);
        ShapeDrawable shapeDrawable1 = new ShapeDrawable();
        shapeDrawable1.setShape(roundRectShape1);
        shapeDrawable1.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable1.getPaint().setColor(Color.parseColor(missionsBean.getProgressbar().getProgress_color()));
        ClipDrawable clipDrawable = new ClipDrawable(shapeDrawable1, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        RoundRectShape roundRectShape0 = new RoundRectShape(outerR, null, null);
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        shapeDrawable.setShape(roundRectShape0);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable.getPaint().setColor(Color.parseColor(missionsBean.getProgressbar().getDefault_color()));

        Drawable[] layers = new Drawable[]{shapeDrawable, clipDrawable};
        LayerDrawable layerDrawable = new LayerDrawable(layers);
        layerDrawable.setId(0, android.R.id.background);
        layerDrawable.setId(1, android.R.id.progress);
        setProgressDrawable(layerDrawable);
    }
}
